// pages/room/yuding/index.js
import dayjs from 'dayjs';
const formatDate = 'YYYY-MM-DD';
import axios from '../../../utils/axios';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageDate:dayjs().format(formatDate),
    dateVisible: false,
    pickerStart: Date.now(),
    timeItems: [],
    roomUsaged: [],
    actived: [],
  },
  toOrder(e){
    const {u,type} = e.currentTarget.dataset;
    if(u === "all"){
      wx.navigateTo({
        url: '/pages/room/yuding/orderList/index?id='+this.roomId+"&roomName="+this.roomName,
      })
      return
    }
    if(type === "active"){
      const {pageDate,roomUsaged} = this.data;
      const curTime = pageDate+" "+u.time+":00";
      let curTimer = {};
      roomUsaged.forEach(item=>{
        if(curTime<=item.end_time && curTime>=item.start_time){
          curTimer=item;
        }
      })
      wx.navigateTo({
        url: '/pages/room/yuding/orderList/index?id='+this.roomId+"&start_time="+curTimer.start_time+"&end_time="+curTimer.end_time+"&roomName="+this.roomName,
      })
      return
    }
  },
   /**
   * 展示时间选择
   */
  showPicker() {
    this.setData({
      dateVisible: true
    });
  },
  /**
   * 获取已使用的时间段
   * @param {option item} item
   */
  async getRoomUsaged(id) {
    const {
      data
    } = await axios.get('/app/getRoomUsaged', {
      roomId: id
    });
    if (data.length > 0) {
      this.setData({
        roomUsaged: data
      });
      this.setData({
        used: []
      });
    }
    this.setTimeItems(this.data.pageDate)
  },
/**
   * 隐藏时间选择
   */
  hidePicker() {
    this.setData({
      dateVisible: false
    });
  },

  /**
   * 切换当前的日期
   * @param {e}
   */
  onConfirm(e) {
    const {
      value
    } = e.detail;
    this.setData({
      pageDate: value
    })
    this.setTimeItems(value);
    this.hidePicker();
  },
  setTimeItems(date = dayjs().format(formatDate)) {
    let times = [];
    for (let hour = 0; hour <= 23; hour++) {
      for (let minute = 0; minute <= 30; minute += 30) {
        let time = `${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}`;
        let currentTime = dayjs(`${date} ${time}`);
        let type = currentTime.isBefore(dayjs()) ? "disabled" : "";
        this.data.roomUsaged.forEach(usage => {
          let startTime = dayjs(usage.start_time).subtract(1, "second");
          let endTime = dayjs(usage.end_time).add(1, "second");
          if (startTime.isBefore(currentTime) && currentTime.isBefore(endTime)) {
            type = "active";
          }
        });
        // this.data.actived.forEach(usage => {
        //   let startTime = dayjs(usage.startTime).subtract(1, "second");
        //   let endTime = dayjs(usage.endTime).add(1, "second");
        //   if (startTime.isBefore(currentTime) && currentTime.isBefore(endTime)) {
        //     type = "active";
        //   }
        // });
        times.push({
          type,
          time
        });
      }
    }
    let duration = 0;
    if (this.data.actived.length > 0) {
      duration = dayjs(this.data.actived[0].endTime).diff(dayjs(this.data.actived[0].startTime)) / 60000;
    }
    this.setData({
      timeItems: times,
      duration: duration / 60
    })
  },
  handleTimeBtn(e){
    const {cur} = e.currentTarget.dataset;
    console.log(cur);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: options.roomName
    })
    this.roomName = options.roomName;
    this.roomId = options.id;
    // this.setTimeItems()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getRoomUsaged(this.roomId);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})